{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4bfecd69",
   "metadata": {},
   "source": [
    "## 介绍Selenium\n",
    "\n",
    "自动化测试工具，支持多浏览器，爬虫中主要来解决js渲染的问题"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7b69437",
   "metadata": {},
   "source": [
    "pip install selenium\n",
    "\n",
    "- 驱动下载地址：\n",
    "http://chromedriver.storage.googleapis.com/index.html\n",
    "\n",
    "- api文档：\n",
    "https://selenium-python.readthedocs.io/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "428bcd05",
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.webdriver.support.wait import WebDriverWait\n",
    "\n",
    "browser = webdriver.Chrome()\n",
    "\n",
    "try:\n",
    "    browser.get('https://www.baidu.com')\n",
    "#     input = browser.find_element(By.ID, 'kw')\n",
    "    input = browser.find_element(by=By.CSS_SELECTOR, value='#kw')\n",
    "    input.send_keys('Python')\n",
    "    input.send_keys(Keys.ENTER)\n",
    "    wait = WebDriverWait(browser, 10)\n",
    "    wait.until(\n",
    "        EC.presence_of_element_located((By.ID,\"content_left\"))\n",
    "    )\n",
    "    print(browser.current_url)\n",
    "    print(browser.get_cookies())\n",
    "    print(browser.page_source)\n",
    "finally:\n",
    "    print('finally')\n",
    "#     browser.close()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0506027d",
   "metadata": {},
   "source": [
    "## 声明浏览器对象\n",
    "- webdriver.Chrome() \n",
    "- webdriver.Firefox()\n",
    "- webdriver.Edge() \n",
    "- webdriver.Safari()\n",
    "\n",
    "## 访问页面\n",
    "browser.get('https://www.baidu.com')\n",
    "\n",
    "## 查找元素\n",
    "\n",
    "- find_element\n",
    "- find_elements\n",
    "\n",
    "## 元素交互操作\n",
    "\n",
    "- input.send_keys()\n",
    "- input.clear()\n",
    "- ele.click()\n",
    "\n",
    "## 浏览器交互动作\n",
    "\n",
    "将动作附加到动作链中串行执行\n",
    "\n",
    "ActionChains\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c41e781",
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver import ActionChains\n",
    "\n",
    "browser = webdriver.Chrome()\n",
    "url = \"https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable\"\n",
    "browser.get(url)\n",
    "browser.switch_to.frame(\"iframeResult\")\n",
    "source = browser.find_element(By.CSS_SELECTOR, '#draggable' )\n",
    "target = browser.find_element(By.CSS_SELECTOR, '#droppable' )\n",
    "actions = ActionChains(browser)\n",
    "actions.drag_and_drop(source, target)\n",
    "actions.perform()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f26b205",
   "metadata": {},
   "source": [
    "## 执行javascript\n",
    "\n",
    "browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')\n",
    "browser.execute_script('alert('to bottom')')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6eba952",
   "metadata": {},
   "source": [
    "## 获取元素信息\n",
    "\n",
    "ele.get_attribute('class')\n",
    "ele.text\n",
    "\n",
    "ele.id\n",
    "ele.location # 在浏览器中占的位置\n",
    "ele.tag_name\n",
    "ele.size # 在当前浏览器下占用的高度宽度\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d4ab600",
   "metadata": {},
   "source": [
    "## Frame\n",
    "\n",
    "browser.switch_to.frame(\"frameId\")\n",
    "browser.switch_to.parent_frame()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6a78bb2",
   "metadata": {},
   "source": [
    "## 等待\n",
    "\n",
    "### 隐式等待\n",
    "\n",
    "隐式等待，如果selenium没有在DOM中找到节点，将继续等待，超出预定时间后就抛出找不到节点的异常。默认是0\n",
    "\n",
    "browser.implicitly_wait(10) \n",
    "\n",
    "### 显式等待\n",
    "\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "\n",
    "wait=WebDriverWait(bro,10)\n",
    "input = wait.until(EC.presence_of_element_located((By.ID,\"q\")))\n",
    "\n",
    "title_is\n",
    "title_contains\n",
    "presence_of_element_located\n",
    "visiblity_of_lelment_located\n",
    "visibleity_of\n",
    "text_to_be_present_in_element\n",
    "text_to_be_present_in_leement_value\n",
    "frame_to_be_available_and_switch_to_ite\n",
    "invisiblity_of_element_located\n",
    "element_to_te_clickable\n",
    "staleness_of\n",
    "element_to_be_clickable\n",
    "selement_to_be_selected\n",
    "element_located_to_be_selected\n",
    "element_selection_state_to_be\n",
    "element_located_selection_state_to_be\n",
    "alert_is_present 是出现alert\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "247ea6d5",
   "metadata": {},
   "source": [
    "## 浏览器前进后退\n",
    "\n",
    "browser.get()\n",
    "browser.back()\n",
    "browser.sleep(1)\n",
    "browser.forward()\n",
    "browser.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbe2b2fe",
   "metadata": {},
   "source": [
    "## Cookies\n",
    "\n",
    "browser.get_cookies()\n",
    "browser.add_cookie({ 'name': 'name', 'domain': 'www.zhihu.com', 'value': 'germey'})\n",
    "browser.get_cookies()\n",
    "browser.delete_all_cookies()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dff03a1",
   "metadata": {},
   "source": [
    "## 选项卡管理\n",
    "\n",
    "browser.execute('window.open()')\n",
    "print(browser.window_handles)\n",
    "browser.switch_to_window(browser.window_handles[1])\n",
    "browser.get(url)\n",
    "browser.sleep(1)\n",
    "browser.switch_to_window(browser.window_handles[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b77a134e",
   "metadata": {},
   "source": [
    "## 异常处理\n",
    "\n",
    "from selenium.common.exceptions import TimeoutException, NoSuchElementException\n",
    "\n",
    "try: \n",
    "\n",
    "except NoSuchElementException:\n",
    "\n",
    "try:\n",
    "\n",
    "except NoSuchElementException:\n",
    "    print('NoSuchElementException')\n",
    "finally: \n",
    "  browser.close()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
